<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/17
 * Time: 9:47
 */
namespace app\api\model;
class Address extends BaseModel
{
    protected $table = 'yoshop_user_address';

    /**
     * 获取用户所有收货地址
     *
     * @param $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserAddress($user_id)
    {
        $list = $this->getList(['user_id'=>$user_id]);
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }

    /**
     * 检查是否有默认收货地址
     *
     * @param $user_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkIsDefault($user_id)
    {
        $res = $this->getInfo(['user_id'=>$user_id,'is_default'=>1]);
        return $res;
    }
    /**
     * 地址详情
     *
     * @param $address_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAddressInfo($user_id,$address_id)
    {
        $data = $this->getInfo(['address_id'=>$address_id,'user_id'=>$user_id]);
        if($data){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$data];
        }else{
            $status = ['code'=>0,'msg'=>'地址不存在','data'=>''];
        }
        return $status;
    }

    /**
     * 绑定/编辑收货地址
     *
     * @param $user_id
     * @return array
     */
    public function bindAddress($user_id)
    {
        $data = input('post.');
        $data['user_id'] = $user_id;
        $validate = new \app\api\validate\Address();
        if(!$validate->check($data)){
            AjaxReturn(0,$validate->getError());
        }
        $this->startTrans();
        if($data['is_default']==1){//如果此地址为默认
            $updataDefault = $this->updateInfo(['user_id'=>$user_id],['is_default'=>0]);//其他地址设置为不默认
        }else{
            $updataDefault = true;
        }
        if($data['address_id']){//修改
            $res = $this->updateInfo(['address_id'=>$data['address_id'],'user_id'=>$user_id],$data);
            $updatePower = true;
        }else{//添加
            //判断是否还有其他地址
            $checkOne = $this->where('user_id',$user_id)->find();
            if(empty($checkOne)){
                //增加算力值
                $config = getSetting();
                $updatePower = $this->user->updateNumber(['user_id'=>$data['user_id']],1,'power',$config['address_number']);
                //增加用户算力记录
                $powerLog = suanli($data['user_id'],$config['address_number']);
            }else{
                $updatePower = true;
                $powerLog = true;
            }
            $res = $this->insertInfo($data);

        }
        if($updataDefault!==false && $res && $updatePower && $powerLog){
            $this->commit();
            $status = ['code'=>1,'msg'=>'编辑成功'];
        }else{
            $this->rollback();
            $status = ['code'=>0,'msg'=>'操作失败'];
        }
        return $status;
    }
}